package com.ruoyi.web.controller.tool.websocket;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.Base64Utils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/websocket")
public class WebsocketController extends BaseController {

	@GetMapping("demo")
	public String socket() {
		System.out.println("进来了-----");
		return "websocket";
	}

	// 推送数据接口
	@ResponseBody
	@RequestMapping("/push/{cid}")
	public Map pushToWeb(@PathVariable String cid, String message) throws IOException {
		if (message == null) {
			message = "我是消息";
		}

		if (cid.equals("topic_top")) {
			WebSocketServer.sendInfo(message, null);
		} else {
			WebSocketServer.sendInfo(message, cid);
		}

		@SuppressWarnings("rawtypes")
		Map<String, Comparable> result = new HashMap<String, Comparable>();
		result.put("code", 200);
		result.put("msg", "success");
		return result;

	}

	// 推送数据接口
	@ResponseBody
	@RequestMapping("/notice/push/{cid}")
	public Map pushToApp(@PathVariable String cid, String message) throws IOException {
		if (message == null) {
			message = "我是消息";
		}
		if (cid.equals("topic_top")) {
			WebSocketServer.sendInfo(Base64Utils.decryptBASE64(message.replaceAll(" ", "+")), null);
		} else {
			WebSocketServer.sendInfo(Base64Utils.decryptBASE64(message.replaceAll(" ", "+")), cid);
		}

		@SuppressWarnings("rawtypes")
		Map<String, Comparable> result = new HashMap<String, Comparable>();
		result.put("code", 200);
		result.put("msg", "success");
		return result;

	}
}
